import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const shoperStore = createSlice({
  name: "counter",
  initialState: {
    num: 0,
    list: [],
  },
  reducers: {
    inscrementNum(state, { payload }) {
      state.num += payload;
    },
    decrementNum(state, { payload = 0 }) {
      state.num--;
    },
    setList(state, { payload = [] }) {
      state.list = payload;
    },
  },
});

const { inscrementNum, decrementNum, setList } = shoperStore.actions;
const reducer = shoperStore.reducer;

// 异步请求数据
const getList = () => {
  return async (dispatch) => {
    const res = await axios.get("http://geek.itheima.net/v1_0/channels");
    dispatch(setList(res.data.data.channels));
  };
};

export { inscrementNum, decrementNum, setList, getList };
export default reducer;
